//
//  ViewController.m
//  copyAndMutableCopy
//
//  Created by 梁宇航 on 17/4/23.
//  Copyright © 2017年 梁宇航. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic,strong)NSArray *strongArray;

//@property (nonatomic,strong)NSMutableArray *myArrayM;

@end

@implementation ViewController{
    
    //本文主角 - 可变数组
    NSMutableArray *_myArrayM;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    //NSArrayUserStrongPropertyDemo
    
    //Demo1:array(copy & mutableCopy)
    
    [self mutableCopyArrayDemo];
    
}

//NSArray 使用 ‘strong’ 修饰可能产生的后果
-(void)NSArrayUserStrongPropertyDemo{

    NSMutableArray *arrayM = @[@1,@2,@3].mutableCopy;
    
    self.strongArray = arrayM;
    
    NSLog(@"1-add之前 ： self.strongArray = %@",self.strongArray);
    
    NSLog(@"------------------------我是分割线-------------------");
    
    [arrayM addObject:@"test"];
    
    //self.strongArray = arrayM;
    
    NSLog(@"2-add之后 ： self.strongArray = %@",self.strongArray);
}
-(void)mutableCopyArrayDemo{

    _myArrayM = [NSMutableArray arrayWithCapacity:10];
    
    NSArray *array = @[@"a",@"b",@"c"];

    //模拟网络请求 - dic ：数据源
    NSDictionary *dic = @{@"key":array};

    NSArray *startArray = dic[@"key"];
    
    [_myArrayM addObjectsFromArray:startArray];
    
    
    //改：
    //_myArrayM = [dic[@"key"] mutableCopy];
    
    NSLog(@"1->_myArrayM = %@",_myArrayM);
    
    //添加对象
    
    NSArray *array2 = @[@1,@2,@3];
    
#pragma mark 万恶之源 - mutableCopy

    [_myArrayM.mutableCopy addObject:array2];
    
    //[_myArrayM addObjectsFromArray:array2];
    
    NSLog(@"2->_myArrayM = %@",_myArrayM);
    
    
    //改3：添加数组初始化！
    _myArrayM = [NSMutableArray arrayWithArray:_myArrayM];
    
    [_myArrayM addObject:array2];
    
    NSLog(@"3->_myArrayM = %@",_myArrayM);
    
    
    //改4：使用新数组去接收_myArrayM，在赋值回去
    
    NSMutableArray *tempArrayM = [NSMutableArray arrayWithArray:_myArrayM];
    
    _myArrayM = tempArrayM;
    
    [_myArrayM addObject:array2];
    
    NSLog(@"4->_myArrayM = %@",_myArrayM);
    
    //改5：将可变的数组_myArrayM当成不可变的来用！
    
    NSMutableArray *tempArrayM2 = [NSMutableArray arrayWithArray:_myArrayM];
    
    [tempArrayM2 addObject:array2];
    
    _myArrayM = tempArrayM2;
    
    NSLog(@"5->_myArrayM = %@",_myArrayM);
    
    //改6:addObjectsFromArray方法
    NSMutableArray *tempArrayM3 = [NSMutableArray arrayWithArray:_myArrayM];
    
    
    [tempArrayM3 addObjectsFromArray:array2];
        _myArrayM = tempArrayM3;
    
        NSLog(@"6->_myArrayM = %@",_myArrayM);
    
    //改7：尝试arrayByAddingObjectsFromArray方法
    NSMutableArray *tempArrayM4 = [NSMutableArray arrayWithArray:_myArrayM];
    
    
    [tempArrayM4 arrayByAddingObjectsFromArray:array2];
    _myArrayM = tempArrayM4;
    
    NSLog(@"7->_myArrayM = %@",_myArrayM);
    
    
    
    //test -arrayByAddingObjectsFromArray
    
//    NSMutableArray *testArrayM = @[@1,@2,@3];
//    
//    [testArrayM arrayByAddingObject:@4];
    
    
    //----------------------------------

    NSArray *testArray = @[@1,@2,@3];
    
    [testArray arrayByAddingObject:@4];
    
    NSLog(@"testArray = %@",testArray);
    
    
}


@end
